﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using TY.Utilities;
using System.Collections;
using System.IO;

namespace TY_75Stability.WinApp
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            //int ff= -8000;

            
            //label6.Text =PIDData.ReadTem("E0C0").ToString();
            //return;
            var t1 = ReadHelper.ReadTxtFile("E:\\css\\3.json");
            var r1 = JsonHelper.JonsToList<ArrayList>(t1);
            var tt1 = r1.Select(x => DateTimeHelper.GetTime(x[0].ToString())).ToList();
            var tt2 = r1.Select(x => Convert.ToInt64(x[0].ToString())).ToList();
            var d4 = r1.Select(x => Utils.ObjToFloat(x[1], 0)).ToList();
            //TY.Algorithm.DataRealtimeStability dreal = new TY.Algorithm.DataRealtimeStability();
            //dreal.Init(0.95);
            //List<double> caleList = new List<double>();
            //double cz = 0;
            //List<float> avgList = new List<float>();

            //List<double[]> txtList = new List<double[]>();
            //var s1 = ReadHelper.ReadTxtFile("E:\\css\\1.txt");
            List<float> d1 = new List<float>();
            List<float> d2 = new List<float>();
            List<float> d3 = new List<float>();
            StreamReader sr = new StreamReader("E:\\css\\1.txt"); while (true)
            {
                string str = sr.ReadLine();
                if (String.IsNullOrEmpty(str))
                {
                    break;
                }
                var ddd = Utils.SplitString(str, "  ");
                d1.Add(Utils.ObjToFloat(ddd[1].Trim(), 0));
                d2.Add(Utils.ObjToFloat(ddd[2].Trim(), 0));
                d3.Add(Utils.ObjToFloat(ddd[3].Trim(), 0));
            }
            string f = "fdfd";
            //for (int i = 0; i < d1.Count; i++)
            //{

            //    //double zhi = 0;
            //    //if ((int)cz == 0)
            //    //{
            //    //    avgList.Add(d1[i]);
            //    //    cz = d1[i];


            //    //}
            //    //else
            //    //{
            //    //    zhi = d1[i] - ((d1[i] - cz) * 0.99);
            //    //    avgList.Add((float)Math.Round(zhi, 3));
            //    //    cz = zhi;
            //    //}
            //    //var f = d1[i];
            //    //double fff;
            //    //if (f < 75)
            //    //{
            //    //    var ggg = 75 - f;

            //    //    fff = f + (75 - f) * (0.7);
            //    //}
            //    //else
            //    //{
            //    //    fff = f - (f - 75) * 0.7;
            //    //}
            //    txtList.Add(new double[] { tt2[i], Math.Round(dreal.AppendData(d1[i]), 3) });
            //}

            ////var ddddd = avgList.Max();
            ////var xxxxx = avgList.Min();
            ////var gg = avgList.Average();
            ////d1 = avgList;
            ////d1.Sort();
            //File.WriteAllText("E:\\css\\3.json", JSON.Encode(txtList));
            //return;
            //d1.RemoveRange(0, 10);
            //d1.RemoveRange(d1.Count - 10, 10);
            //label4.Text = label4.Text + d1.Max();
            //label5.Text = label5.Text + d1.Min();
            //label3.Text = label3.Text + Math.Round(d1.Max() - d1.Min(), 3);
            //label6.Text = label6.Text + d1.Average();

            //var t2 = ReadHelper.ReadTxtFile("E:\\新建文件夹 (14)\\1_2.json");
            //var r2 = JsonHelper.JonsToList<ArrayList>(t2);
            //var d2 = r2.Select(x => Utils.ObjToFloat(x[1], 0)).ToList();
            //d2.Sort();


            //d2.RemoveRange(0, 10);
            //d2.RemoveRange(d2.Count - 10, 10);
            //label8.Text = label8.Text + d2.Max();
            //label7.Text = label7.Text + d2.Min();
            //label14.Text = label14.Text + Math.Round(d2.Max() - d2.Min(), 2);
            //label9.Text = label9.Text + d2.Average();

            //var t3 = ReadHelper.ReadTxtFile("E:\\新建文件夹 (14)\\1_3.json");
            //var r3 = JsonHelper.JonsToList<ArrayList>(t3);
            //var d3 = r3.Select(x => Utils.ObjToFloat(x[1], 0)).ToList();
            //d3.Sort();


            //d3.RemoveRange(0, 10);
            //d3.RemoveRange(d3.Count - 10, 10);
            //label12.Text = label12.Text + d3.Max();
            //label11.Text = label11.Text + d3.Min();
            //label15.Text = label15.Text + Math.Round(d3.Max() - d3.Min(), 3);
            //label10.Text = label10.Text + d3.Average();

            StreamWriter sw = new StreamWriter("E:\\css\\6.txt", true, System.Text.Encoding.Default);

            Random r = new Random();
            for (int i = 0; i < d1.Count; i++)
            {
                sw.WriteLine(string.Format("{0}   {1}   {2}   {3}   {4}", tt1[i], d1[i], d2[i], d3[i], d4[i]));
                int v1 = 10;
                int v2 = 10;
                int v3 = 10;
                int v4 = 10;

                int b1 = 1;
                int b2 = 1;
                int b3 = 1;
                int b4 = 1;
                if (i < d1.Count - 1)
                {
                    v1 = Convert.ToInt32(Math.Round(d1[i + 1] - 74.963, 3) * 1000);
                    if (v1 < 0)
                    {
                        v1 = Math.Abs(v1);
                        b1 = -1;
                    }
                    v2 = Convert.ToInt32(Math.Round(d2[i + 1] - d1[2], 3) * 1000);
                    if (v2 < 0)
                    {
                        v2 = Math.Abs(v2);
                        b2 = -1;
                    }
                    v3 = Convert.ToInt32(Math.Round(d3[i + 1] - d1[3], 3) * 1000);
                    if (v3 < 0)
                    {
                        v3 = Math.Abs(v3);
                        b3 = -1;
                    }
                    v4 = Convert.ToInt32(Math.Round(d4[i + 1] - d1[4], 3) * 1000);
                    if (v4 < 0)
                    {
                        v4 = Math.Abs(v4);
                        b4 = -1;
                    }
                }
                for (int j = 0; j < 4; j++)
                {
                    sw.WriteLine(string.Format("{0}   {1}   {2}   {3}   {4}", tt1[i], Math.Round(d1[i] + r.Next(0, v1) * b1 / 1000.0, 3), Math.Round(d2[i] + r.Next(0, v2) * b2 / 1000.0, 2), Math.Round(d3[i] + r.Next(0, v3) * b3 / 1000.0, 3), Math.Round(d4[i] + r.Next(0, v4) * b4 / 1000.0, 3)));
                }
            }
            sw.Close();
        }
    }
}
